﻿using Demo.DemoFeature.ApiModels.Traces;
using Demo.DemoFeature.DBModels;

using GCC.APIContainer.Auth.Attributes;
using GCC.APIContainer.Utilities;
using GCC.Container.Abstracts;
using GCC.Container.Models;

using Microsoft.AspNetCore.Mvc;

namespace Demo.DemoFeature
{
    [ApiController]
    [Route("Demo")]
    public class TraceInfoController : GCCAPIControllerBase
    {
        public TraceInfoController(AbsGCCContext context) : base(context)
        {
        }

        [HttpGet("TraceInfo/items")]
        [GCCAuth(allowOffline: true, roles: new string[] { "admin" })]
        public DataResponse<List<GetAllTraceItemResponse>> GetAllTraceItem([FromQuery] GetAllTraceItemRequest request)
        {
            var dbres = GCCEntrance.DBManager!.LocalDB!.Select<TraceItem>()
                .WhereIf(!string.IsNullOrWhiteSpace(request.PointName), item => item.PointName.Equals(request.PointName!))
                .WhereIf(!string.IsNullOrWhiteSpace(request.Type), item => item.Type.Equals(request.Type!))
                .WhereIf(!string.IsNullOrWhiteSpace(request.SessionID), item => item.SessionID.Equals(request.SessionID!))
                .WhereIf(!string.IsNullOrWhiteSpace(request.ResourceName), item => item.ResourceName.Equals(request!.ResourceName!))
                .WhereIf(!string.IsNullOrWhiteSpace(request.Value), item => item.Value.Equals(request.Value!))
                .WhereIf(!string.IsNullOrWhiteSpace(request.Path), item => item.Path.Equals(request.Path!))
                .WhereIf(!string.IsNullOrWhiteSpace(request.From), item => item.From.Equals(request.From!))
                .WhereIf(request.Begin != null && request.End != null, item => item.Created > request.Begin!.Value.Ticks && item.Created < request.End!.Value.Ticks)
                .OrderByDescending(item => item.Created)
                // .Count(out var count)
                .Page(request.PageIndex ?? 1, request.PageSize)
                .ToList();
            return HandleData(dbres.Select(item => new GetAllTraceItemResponse()
            {
                Created = new DateTime(item.Created, DateTimeKind.Utc).ToLocalTime(),
                From = item.From,
                Method = item.Method,
                Path = item.Path,
                PointName = item.PointName,
                ResourceName = item.ResourceName,
                SessionID = item.SessionID,
                Type = item.Type,
                Value = item.Value,
            }).ToList(), 100);
        }

        [HttpGet("TraceInfo/items/process/cpu")]
        [GCCAuth(allowOffline: true, roles: new string[] { "admin" })]
        public DataResponse<GetProcessCpuInfoResponse> GetProcessCpuInfo([FromQuery] GetProcessCpuInfoRequest _)
        {

            var dbres = GCCEntrance.DBManager!.LocalDB!.Select<TraceItem>()
                .Where(item => item.Type == "Process" && item.ResourceName == "Cpu Age")
                .Where(item => item.Created > DateTime.UtcNow.AddMinutes(-60).Ticks && item.Created < DateTime.UtcNow.Ticks)
                .OrderByDescending(item => item.Created)
                .ToList();

            var res = new GetProcessCpuInfoResponse();
            dbres.Reverse();
            dbres.ForEach(item =>
            {

                res.PointInfos.Add(new GetProcessCpuInfoResponse.GetProcessCpuItem()
                {
                    Value = decimal.Round(decimal.Parse(item.Value), 2),
                    Time = new DateTime(item.Created, DateTimeKind.Utc).ToLocalTime(),
                    PointName = item.PointName,
                });
            });


            return HandleData(res);
        }

        [HttpGet("TraceInfo/items/process/privateMem")]
        [GCCAuth(allowOffline: true, roles: new string[] { "admin" })]
        public DataResponse<GetProcessPrivateMemInfoResponse> GetProcessPrivateMemInfo([FromQuery] GetProcessPrivateMemInfoRequest _)
        {

            var dbres = GCCEntrance.DBManager!.LocalDB!.Select<TraceItem>()
                .Where(item => item.Type == "Process" && item.ResourceName == "Private Memory Size 64")
                .Where(item => item.Created > DateTime.UtcNow.AddMinutes(-60).Ticks && item.Created < DateTime.UtcNow.Ticks)
                .OrderByDescending(item => item.Created)
                .ToList();

            var res = new GetProcessPrivateMemInfoResponse();
            dbres.Reverse();
            dbres.ForEach(item =>
            {

                res.PointInfos.Add(new GetProcessPrivateMemInfoResponse.GetProcessPrivateMemInfoItem()
                {
                    Value = decimal.Round(decimal.Parse(item.Value), 2),
                    Time = new DateTime(item.Created, DateTimeKind.Utc).ToLocalTime(),
                    PointName = item.PointName,
                });
            });


            return HandleData(res);
        }

        [HttpGet("TraceInfo/items/process/PagedMem")]
        [GCCAuth(allowOffline: true, roles: new string[] { "admin" })]
        public DataResponse<GetProcessPagedMemInfoResponse> GetProcessPagedMemInfo([FromQuery] GetProcessPagedMemInfoRequest _)
        {

            var dbres = GCCEntrance.DBManager!.LocalDB!.Select<TraceItem>()
                .Where(item => item.Type == "Process" && item.ResourceName == "Paged Memory Size 64")
                .Where(item => item.Created > DateTime.UtcNow.AddMinutes(-60).Ticks && item.Created < DateTime.UtcNow.Ticks)
                .OrderByDescending(item => item.Created)
                .ToList();

            var res = new GetProcessPagedMemInfoResponse();
            dbres.Reverse();
            dbres.ForEach(item =>
            {

                res.PointInfos.Add(new GetProcessPagedMemInfoResponse.GetProcessPagedMemInfoItem()
                {
                    Value = decimal.Round(decimal.Parse(item.Value), 2),
                    Time = new DateTime(item.Created, DateTimeKind.Utc).ToLocalTime(),
                    PointName = item.PointName,
                });
            });


            return HandleData(res);
        }
    }
}
